from login import use_default_login
from collect import CollectThread
from generate import ReportGenerator
import data_writing_json
import utils
import os


def collect_and_generate(date=utils.get_yesterday(), make_type='all', thread=8):
    library = utils.default_library()
    service = use_default_login()
    account_chunks = library.get_account_chunks(thread)
    chunk_count = 1
    print('获取并生成{}的报表'.format(date))
    for chunk in account_chunks:
        print('=第{}批线程开始='.format(chunk_count))
        pool = []
        for gh_id in chunk:
            account = library.get_account_data(gh_id)
            thread = CollectThread(service, account, date)
            thread.start()
            pool.append(thread)
        for thread in pool:
            thread.join()
        print('=第{}/{}批线程结束='.format(chunk_count, len(account_chunks)))
        chunk_count += 1
    print('=生成报告=')
    if make_type == 'ad':
        reporter = ReportGenerator(library, utils.get_today())
        try:
            reporter.make_today_ad()
        except Exception as e:
            print(e)
    else:
        try:
            reporter = ReportGenerator(library, date)
            reporter.make_all()
        except Exception as e:
            print(e)

    print("=数据开始写入数据库中=")
    try:
        data_writing_json.json_insert_Data_form_table()
        os.remove("Data_form_table.json")
        print("数据表格 全部数据 完成写入mysql库操作")
    except Exception as e:
        print(e)
        print("数据表格 数据写入mysql库 出错")
    try:
        data_writing_json.json_insert_Advertiser_data_form()
        os.remove("Advertiser_data_form.json")
        print("广告主表格 全部数据 完成写入mysql库操作")
    except Exception as e:
        print(e)
        print("广告主表格 数据入mysql库 出错")
    try:
        data_writing_json.json_insert_reading_comparison_12_hour_table()
        os.remove("insert_reading_comparison_12_hour_table.json")
        print("12小时 阅读对比 全部数据 完成写入mysql库操作")
    except:
        print("12小时 阅读对比 数据入mysql库 出错")
    try:
        data_writing_json.json_insert_reading_comparison_36_hour_table()
        os.remove("insert_reading_comparison_36_hour_table.json")
        print("36小时 阅读对比 全部数据 完成写入mysql库操作")
    except:
        print("36小时 阅读对比 数据入mysql库 出错")
    try:
        data_writing_json.json_insert_Violation_data_table()
        os.remove("Violation_data_table.json")
        print("违规数据 全部数据 完成写入mysql库操作")
    except:
        print("违规数据 没有数据 写入mysql库")

    input('=运行结束，按回车键退出=')


if __name__ == '__main__':
    date = None
    yesterday = utils.get_yesterday()
    print("数据收集日期默认为：{}".format(yesterday))
    # date = input('请输入收集日期,默认为昨天({}): '.format(yesterday))
    collect_and_generate(date if date else yesterday)
